<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/29
 * Time: 14:34
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Service\Admin\OrderService;
use App\Constant\ErrorConstant;

class OrderController extends BaseController
{
    protected $service;

    public function __construct(Request $request, OrderService $service)
    {
        parent::__construct($request);
        $this->service = $service;
    }

    /**
     * 订单列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listOrder()
    {
        $query = $this->request->post('query', []);
        $limit = $this->request->input('page_size', NULL);
        $page = $this->request->input('page', 1);

        [$flag, $result] = $this->service->orderList($query, $limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_LIST_ERROR, $result);
        }
    }

    /**
     * 订单详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_id'  =>  'required|integer|exists:mini_order,id'
        ], [
            'order_id.required' =>  '订单ID不能为空',
            'order_id.integer'  =>  '订单ID必须为整型',
            'order_id.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_DETAIL_ERROR, $validator->errors()->first());
        }

        $order_id   =   (int) $this->request->post('order_id');
        [$flag, $result]    =   $this->service->orderDetail($order_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_DETAIL_ERROR, $result);
        }
    }

    /**
     * 订单退款
     * @return \Illuminate\Http\JsonResponse
     */
    public function refundOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_id'  =>  'required|integer|exists:mini_order,id',
            'detail_id' =>  'required|integer|exists:mini_order_product,id',
            'reason'    =>  'required|string',
        ], [
            'order_id.required' =>  '订单ID不能为空',
            'order_id.integer'  =>  '订单ID必须为整型',
            'order_id.exists'   =>  '订单不存在',
            'detail_id.required'=>  '订单产品ID不能为空',
            'detail_id.integer' =>  '订单产品ID必须为整型',
            'detail_id.exists'  =>  '订单产品不存在',
            'reason.required'   =>  '取消原因不能为空',
            'reason.string'     =>  '取消原因必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_REFUND_ERROR, $validator->errors()->first());
        }

        $order_id   =   (int) $this->request->post('order_id');
        $detail_id  =   (int) $this->request->post('detail_id');
        $reason     =   $this->request->post('reason');
        [$flag, $result]    =   $this->service->orderRefund($order_id, $detail_id, $reason);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_REFUND_ERROR, $result);
        }
    }

    /**
     * 订单取消
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancelOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_id'  =>  'required|integer|exists:mini_order,id',
            'reason'    =>  'required|string',
        ], [
            'order_id.required' =>  '订单ID不能为空',
            'order_id.integer'  =>  '订单ID必须为整型',
            'order_id.exists'   =>  '订单不存在',
            'reason.required'   =>  '取消原因不能为空',
            'reason.string'     =>  '取消原因必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_CANCEL_ERROR, $validator->errors()->first());
        }

        $order_id   =   (int) $this->request->post('order_id');
        $reason     =   $this->request->post('reason');
        [$flag, $result]    =   $this->service->orderCancel($order_id, $reason);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_CANCEL_ERROR, $result);
        }
    }

    /**
     * 订单修改备注
     * @return \Illuminate\Http\JsonResponse
     */
    public function remarkOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_id'  =>  'required|integer|exists:mini_order,id',
            'remark'    =>  'required|string',
        ], [
            'order_id.required' =>  '订单ID不能为空',
            'order_id.integer'  =>  '订单ID必须为整型',
            'order_id.exists'   =>  '订单不存在',
            'remark.required'   =>  '取消原因不能为空',
            'remark.string'     =>  '取消原因必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_REMARK_ERROR, $validator->errors()->first());
        }

        $order_id   =   (int) $this->request->post('order_id');
        $remark     =   $this->request->post('remark');
        [$flag, $result]    =   $this->service->orderRemark($order_id, $remark);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_REMARK_ERROR, $result);
        }
    }

    /**
     * 订单发货
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirmOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_id'  =>  'required|integer|exists:mini_order,id',
            'is_express'=>  'required|integer|in:0,1',
            'express_no'=>  'required_if:is_express,1|string',
            'express_id'=>  'required_if:is_express,1|integer|exists:mini_express,id'
        ], [
            'order_id.required' =>  '订单ID不能为空',
            'order_id.integer'  =>  '订单ID必须为整型',
            'order_id.exists'   =>  '订单不存在',
            'is_express.required'   =>  '是否需要快递不能为空',
            'is_express.integer'    =>  '是否需要快递因必须为整型',
            'is_express.in'         =>  '是否需要快递选择错误',
            'express_id.required_if' =>  '快递公司ID不能为空',
            'express_id.integer'  =>  '快递公司ID必须为整型',
            'express_id.exists'   =>  '快递公司不存在',
            'express_no.required_if'   =>  '快递单号不能为空',
            'express_no.string'     =>  '快递单号必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_CONFIRM_ERROR, $validator->errors()->first());
        }

        $order_id   =   (int) $this->request->post('order_id');
        $is_express =   (int) $this->request->post('is_express', 1);
        $express_id =   (int) $this->request->post('express_id', 0);
        $express_no =   $this->request->post('express_no', '');
        [$flag, $result]    =   $this->service->orderConfirm($order_id, $is_express, $express_id, $express_no);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_CONFIRM_ERROR, $result);
        }
    }

}
